//
//  CMDBRegisterBottomView.swift
//  CMDB
//
//  Created by 王磊 on 05/05/2017.
//  Copyright © 2017 CMDB. All rights reserved.
//

import UIKit

class CMDBRegisterBottomView: TSBaseView {
    
    internal var email: CMDBSubRegisterTF = CMDBSubRegisterTF()
    
    internal var pwd: CMDBSubRegisterTF = CMDBSubRegisterTF()
    
    internal var pwdVerify: CMDBSubRegisterTF = CMDBSubRegisterTF()
    
    internal var birthday: CMDBRegisterClickTF = CMDBRegisterClickTF()
    
    internal var trade: CMDBRegisterClickTF = CMDBRegisterClickTF()
    
    internal var sex: CMDBRegisterSexTF = CMDBRegisterSexTF()
    
}
extension CMDBRegisterBottomView {
    
    //    internal func addTargetForkeyboardReturnKey(target: AnyObject,action: Selector) {
    //
    //        unsureTarget = target
    //
    //        keyboardReturnAction = action
    //    }
}
extension CMDBRegisterBottomView {
    
    override func addOwnSubviews() {
        super.addOwnSubviews()
        
        addSubview(email)
        
        addSubview(pwd)
        
        addSubview(pwdVerify)
        
        addSubview(birthday)
        
        addSubview(trade)
        
        addSubview(sex)
    }
    override func configOwnSubviews() {
        super.configOwnSubviews()
        
        let starAttr = [NSFontAttributeName: CMDB_Register_TF_FONT,NSForegroundColorAttributeName: UIColor.Hex6(0xff6d00, alpha: 54),NSKernAttributeName: 1]
        
        let starText = "*"
        
        let textAttr = [NSFontAttributeName: CMDB_Register_TF_FONT,NSForegroundColorAttributeName: word87Color]
        
        let placeHolderAttr = [NSFontAttributeName: CMDB_Register_TF_FONT,NSForegroundColorAttributeName: CMDB_Register_TF_PlaceHolder_Color]
        
        let emailAttr = [NSFontAttributeName: CMDB_Register_TF_FONT,NSForegroundColorAttributeName: UIColor.clearColor(),NSKernAttributeName: 1]
        
        var mutable = NSMutableAttributedString(attributedString: NSAttributedString(string: "*", attributes: emailAttr))
        
        mutable.appendAttributedString(NSMutableAttributedString(attributedString: NSAttributedString(string: "邮箱:", attributes: textAttr)))
        
        email.attrText = mutable
        
        email.placeHolderAttr = NSAttributedString(string: "请输入您的邮箱", attributes: placeHolderAttr)
        
        email.keyboardType = .EmailAddress
        
        email.returnKeyType = .Next
        
        email.maxLength = 30
        
        email.addTarget(self, action: #selector(keyboardReturn), controlEvent: ControlEvent.ReturnKey)
        
        mutable = NSMutableAttributedString(string: starText, attributes: starAttr)
        
        mutable.appendAttributedString(NSAttributedString(string: "登陆密码:", attributes: textAttr))
        
        pwd.attrText = mutable
        
        pwd.placeHolderAttr = NSAttributedString(string: "请输入密码", attributes: placeHolderAttr)
        
        pwd.keyboardType = .Default
        
        pwd.returnKeyType = .Next
        
        pwd.maxLength = 18
        
        pwd.addTarget(self, action: #selector(keyboardReturn), controlEvent: ControlEvent.ReturnKey)
        
        mutable = NSMutableAttributedString(string: starText, attributes: starAttr)
        
        mutable.appendAttributedString(NSAttributedString(string: "确认密码:", attributes: textAttr))
        
        pwdVerify.attrText = mutable
        
        pwdVerify.placeHolderAttr = NSAttributedString(string: "请确认密码", attributes: placeHolderAttr)
        
        pwdVerify.keyboardType = .Default
        
        pwdVerify.returnKeyType = .Next
        
        pwdVerify.maxLength = 18
        
        
        pwdVerify.addTarget(self, action: #selector(keyboardReturn), controlEvent: ControlEvent.ReturnKey)
        
        //
        mutable = NSMutableAttributedString(string: starText, attributes: starAttr)
        
        mutable.appendAttributedString(NSAttributedString(string: "生日:", attributes: textAttr))
        
        birthday.attrText = mutable
        
        birthday.addTarget(self, action: #selector(keyboardReturn), controlEvent: ControlEvent.ReturnKey)
        
        let attr = [NSFontAttributeName: CMDB_Register_TF_FONT,NSForegroundColorAttributeName: UIColor.lightGrayColor()]
        
        birthday.detailLabel.attributedText = NSAttributedString(string: "请选择生日", attributes: attr)
        
        mutable = NSMutableAttributedString(string: starText, attributes: starAttr)
        
        mutable.appendAttributedString(NSAttributedString(string: "入行年份:", attributes: textAttr))
        
        trade.attrText = mutable
        
        trade.addTarget(self, action: #selector(keyboardReturn), controlEvent: ControlEvent.ItemClick)
        
        trade.btn.hidden = true
        
        trade.detailLabel.attributedText = NSAttributedString(string: "请选择入行年份", attributes: attr)
        
        mutable = NSMutableAttributedString(string: starText, attributes: starAttr)
        
        mutable.appendAttributedString(NSAttributedString(string: "性别:", attributes: textAttr))
        
        sex.attrText = mutable
        
        sex.addTarget(self, action: #selector(keyboardReturn), controlEvent: ControlEvent.ReturnKey)
        
    }
}

extension CMDBRegisterBottomView {
    
    @objc private func keyboardReturn(argu: ArguInfo) {
        if argu.view == email {
            
            email.resignFirstResponder()
            
            pwd.becomeFirstResponder()
            
        } else if argu.view == pwd {
            
            pwd.resignFirstResponder()
            
            pwdVerify.becomeFirstResponder()
            
        } else if argu.view == pwdVerify {
            
            pwdVerify.resignFirstResponder()
            
            if let target = unsureTarget {
                
                TSRuntimeManager.getInstance().addTarget(target, andSel: unsureAction!, andAnyArgu: argu)
            }
        } else {
            
            if let target = unsureTarget {
                
                TSRuntimeManager.getInstance().addTarget(target, andSel: unsureAction!, andAnyArgu: argu)
            }
        }
    }
    
}
extension CMDBRegisterBottomView {
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        email.snp_makeConstraints { (make) in
            
            make.left.equalTo(0)
            
            make.right.equalTo(0)
            
            make.top.equalTo(0)
            
            make.height.equalTo(CMDB_Register_TF_Height)
        }
        pwd.snp_makeConstraints { [weak self](make) in
            
            make.left.equalTo(0)
            
            make.right.equalTo(0)
            
            make.top.equalTo(self!.email.snp_bottom)
            
            make.height.equalTo(CMDB_Register_TF_Height)
        }
        
        pwdVerify.snp_makeConstraints { [weak self](make) in
            
            make.left.equalTo(0)
            
            make.right.equalTo(0)
            
            make.top.equalTo(self!.pwd.snp_bottom)
            
            make.height.equalTo(CMDB_Register_TF_Height)
        }
        
        birthday.snp_makeConstraints { [weak self](make) in
            
            make.left.equalTo(0)
            
            make.right.equalTo(0)
            
            make.top.equalTo(self!.pwdVerify.snp_bottom)
            
            make.height.equalTo(CMDB_Register_TF_Height)
        }
        trade.snp_makeConstraints { [weak self](make) in
            
            make.left.equalTo(0)
            
            make.right.equalTo(0)
            
            make.top.equalTo(self!.birthday.snp_bottom)
            
            make.height.equalTo(CMDB_Register_TF_Height)
        }
        
        sex.snp_makeConstraints { [weak self](make) in
            
            make.left.equalTo(0)
            
            make.right.equalTo(0)
            
            make.top.equalTo(self!.trade.snp_bottom)
            
            make.height.equalTo(CMDB_Register_TF_Height)
        }
    }
}